Add TFLITE_DCHECK for buffer index bounds in GetFlatbufferTensorBuffer - #3639
Open
TristanInSec wants to merge 1 commit into
Open
Add TFLITE_DCHECK for buffer index bounds in GetFlatbufferTensorBuffer#3639TristanInSec wants to merge 1 commit into
TristanInSec wants to merge 1 commit into
Conversation
GetFlatbufferTensorBuffer uses flatbuffer_tensor.buffer() as a direct index into the buffers vector without any validation. FlatBuffers Vector::operator[] does not perform bounds checking. A structurally malformed model with a buffer index exceeding the vector size causes an OOB read. Per the Error Handling Guide, FlatBuffer structural bounds checks should use TFLITE_DCHECK exclusively (zero-cost debug aid, compiled out in release). This is consistent with the project's trust model where structurally valid FlatBuffers are the application layer's responsibility. Note: the compression path (line 391) already validates buffer_index against buffers->size() with a runtime check. This TFLITE_DCHECK brings the main path in line for debug builds.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
GetFlatbufferTensorBuffer()usesflatbuffer_tensor.buffer()as a direct index into the FlatBuffersbuffersvector without any validation. FlatBuffersVector::operator[]does not perform bounds checking. A structurally malformed model with a buffer index exceeding the vector size causes an OOB read.Fix
Add a
TFLITE_DCHECKbefore the vector access:This replaces the previous PR #3535 which was closed for not following the Error Handling Guide.
Alignment with Error Handling Guide
Per Section 1 (The FlatBuffer Model - Corrupted FlatBuffer Files):
Per Section 4 (Fixing Fuzzer Crashes - Corrupted FlatBuffer Files):
Note: the compression path at line 391 of the same file already validates
buffer_index >= buffers->size()with a runtime check. This TFLITE_DCHECK brings the main path in line for debug builds, consistent with the guide's zero-overhead philosophy for structural FlatBuffer validation.Testing
Verified with debug build (TFLITE_DCHECK enabled): crafted model with buffer index 100 and only 3 buffers now triggers assertion failure during development, aiding detection of malformed models.
Fixes GHSA-8c4x-xhfq-6wx5